﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace StateManager
{
    class ConfirmBox : GenericScreenCode
    {
        // this class creates a messagebox that asks the user whether he/she wants to quit.
        // If the player hits A or enter, the player is returned back to the main menu.
        // If the player hits B or escape, the player is taken back to the pause menu.
        Texture2D box;
        string message;

        public event EventHandler<PlayerEventHandler> yes;
        public event EventHandler<PlayerEventHandler> no;

        public ConfirmBox(string message)
            : this(message, true)
        {

        }

        public ConfirmBox(string message, bool includeUsage)
        {
            const string usageText = "\n yes = A button or Enter \n no = B button or Escape";

            if (includeUsage)
                this.message = message + usageText;
            else
                this.message = message;

            IsPopup = true;
        }

        public override void LoadContent()
        {
            //base.LoadContent();

            ContentManager content = SManager.Game.Content;

            box = content.Load<Texture2D>("images/ConfirmBox");
        }


        //public override void Update(GameTime gameTime, bool setFocus, bool coveredByOtherScreen)
        //{
        //    //base.Update(gameTime, setFocus, coveredByOtherScreen);
        //}

        public override void HandleInput(Input input)
        {
            PlayerIndex pIndex;
            //base.HandleInput(input);

            if (input.IsMenuSelect(ControllingPlayer, out pIndex))
            {
                if (yes != null)
                    yes(this, new PlayerEventHandler(pIndex));

                ExitScreen();
            }
            else if (input.IsMenuCancel(ControllingPlayer, out pIndex))
            {
                if (no != null)
                    no(this, new PlayerEventHandler(pIndex));

                ExitScreen();
            }
        }

        public override void Draw(GameTime gameTime)
        {
            //base.Draw(gameTime);

            SpriteBatch spriteBatch = SManager.SpriteBatch;
            SpriteFont font = SManager.SpriteFont;

            Viewport view = SManager.GraphicsDevice.Viewport;
            Vector2 viewSize = new Vector2(view.Width, view.Height);
            Vector2 textSize = font.MeasureString(message);
            Vector2 textPosition = (viewSize - textSize) / 2;

            Rectangle backgroundRect = new Rectangle((int)textPosition.X,
                                                     (int)textPosition.Y,
                                                     (int)textSize.X,
                                                     (int)textSize.Y);

            spriteBatch.Begin();

            spriteBatch.Draw(box, backgroundRect, Color.White);

            spriteBatch.DrawString(font, message, textPosition, Color.White);

            spriteBatch.End();
                                                    


        }

    }
}
